package com.abou.mvc.validation.impl;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import com.abou.constant.product.EProductType;
import com.abou.model.common.product.Product;
import com.abou.mvc.validation.annotation.ProductValid;

/**
 * The validator used to check that a product object contains consistent values
 * 
 * @author khalid
 */
public class ProductValidConstraintValidator implements
    ConstraintValidator<ProductValid, Product> {
  
  /**
   * @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
   */
  public void initialize(ProductValid annotation) {

  }
  
  /**
   * @see javax.validation.ConstraintValidator#isValid(java.lang.Object,
   *      javax.validation.ConstraintValidatorContext)
   */
  public boolean isValid(Product product, ConstraintValidatorContext context) {

    boolean isValid = true;
    
    if (product.getProductType() == EProductType.CLOTHING_MAN
        || product.getProductType() == EProductType.CLOTHING_WOMAN
        || product.getProductType() == EProductType.CLOTHING_BOY
        || product.getProductType() == EProductType.CLOTHING_GIRL) {
      
      isValid = product.getClotheType() != null;
    }
    
    if (!isValid) {
      ConstraintValidatorContext.ConstraintViolationBuilder builder =
          context.buildConstraintViolationWithTemplate("");
      builder.addNode("clotheType").addConstraintViolation();
    }
    
    return isValid;
  }
}
